<?php

/*
 * Usage:
* merge_infos($first, $second);
* adds data from the second class in the first
* parameter:
* $first - infos_struct the one to be filled
* $second - infos_struct the one to fill the other
* return:
* infos_struct with added infos
*/
function merge_infos($first, $second) {

    if ($first->id === NULL || $first->id === "")
        $first->id = $second->id;

    if ($first->title === NULL || $first->title === "" || levenshtein($first->title, $second->title) >= 1)
        $first->title = $second->title;

    if ($first->author === NULL || $first->author === "" || strlen($first->author) < strlen($second->author) || levenshtein($first->author, $second->author) >= 1)
        $first->author = $second->author;

    if ($first->year === NULL || $first->year === "") {
        $first->year = $second->year;
        $first->bc = $second->bc;
    }
    if ($first->journal === NULL || $first->journal === "" || levenshtein($first->journal, $second->journal) >= 1)
        $first->journal = $second->journal;
    if ($first->volume === NULL || $first->volume === "" || levenshtein($first->volume, $second->volume) >= 1)
        $first->volume = $second->volume;
    if ($first->number === NULL || $first->number === "" || levenshtein($first->number, $second->number) >= 1)
        $first->number = $second->number;
    if ($first->pages === NULL || $first->pages === "" || levenshtein($first->pages, $second->pages) >= 1)
        $first->pages = $second->pages;
    if ($first->other === NULL || $first->other === "")
        $first->other = $second->other;

    if ($first->note_id === NULL || $first->note_id === "")
        $first->note_id = $second->note_id;
    if ($first->note === NULL || $first->note === "" || strlen($first->note) < strlen($second->note))
        $first->note = $second->note;
    if ($first->note_title === NULL || $first->note_title === "")
        $first->note_title = $second->note_title;

    if ($first->doi === NULL || $first->doi === "")
        $first->doi = $second->doi;
    if ($first->zmath === NULL || $first->zmath === "")
        $first->zmath = $second->zmath;
    if ($first->arxiv === NULL || $first->arxiv === "")
        $first->arxiv = $second->arxiv;

    return $first;
}

/*
 * Usage:
* create_summary($info_struct);
* fills in all infos selected in $info_struct and returns a finish text
* parameter:
* $info_struct - call by ref - new created info_struct just to be filled in with data
* return:
* the finish text to be displayed if gone wrong it returns false
*/
function create_summary(&$info_struct) {

    $finish = true;

    if (isset($_POST['radiozma'])) {
        $zmath = unserialize(base64_decode($_POST['serial'.$_POST['radiozma']]));
    }
    if (isset($_POST['radioarx'])) {
        $arxiv = unserialize(base64_decode($_POST['serial'.$_POST['radioarx']]));
    }
    if (isset($_POST['radiodoi'])) {
        $doi = unserialize(base64_decode($_POST['serial'.$_POST['radiodoi']]));
    }
    if (isset($_POST['radioman'])) {
        $manual = unserialize(base64_decode($_POST['serial'.$_POST['radioman']]));
    }
    if (isset($_POST['noteman0'])) {
        $note_without_title = unserialize(base64_decode($_POST['noteman0']));
    }
    if (isset($_POST['extra_note']) && isset($_POST['extra_note_visible']) && $_POST['extra_note_visible'] != "hidden") {
        $extra_note = new infos();
        $extra_note->note = POST('extra_note');
    }

    // merge data we got
    if (isset($manual)) {
        $info_struct = $manual;
    } else {
        if (isset($arxiv) && $arxiv->rating == NULL) {
            $info_struct = $arxiv;
        } elseif (isset($doi) && $doi->rating == NULL) {
            $info_struct = $doi;
        } elseif (isset($zmath) && $zmath->rating == NULL) {
            $info_struct = $zmath;
        }
    }

    if (isset($doi)) {
        $info_struct = merge_infos($info_struct, $doi);
    }
    if (isset($zmath)) {
        $info_struct = merge_infos($info_struct, $zmath);
    }
    if (isset($arxiv)) {
        $info_struct = merge_infos($info_struct, $arxiv);
    }
    if (isset($note_without_title)) {
        $info_struct = merge_infos($info_struct, $note_without_title);
    }
    if (isset($extra_note)) {
        $info_struct = merge_infos($info_struct, $extra_note);
    }

    if (($info_struct->title === NULL || $info_struct->author === NULL || $info_struct->year === NULL) && ($info_struct->note === NULL)) {
        $finish_text = false;
    } else {
        include_once("../LDB_API/input.php");
        $reference_id = add_ref($info_struct);
        if (!$reference_id) {
            $finish_text = "Error when adding to the database!";
        } else {
            $finish_text = "Your reference has been successfully entered.";
            $info_struct->id = $reference_id;
        }
    }

    return $finish_text;
}

?>